package com.winter.storm.bolt;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.tuple.Tuple;

public class myBolt implements IRichBolt {
    private FileWriter fileWriter;
    @Override
    public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
        // TODO Auto-generated method stub
        try {
            System.out.println("准备filewriter");
            fileWriter = new FileWriter("/home/czq/tmp/please.txt");
            System.out.println("filewriter准备成功");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void execute(Tuple input) {
        // TODO Auto-generated method stub
        System.out.println(input);
        try {
            System.out.println("准备输出");
            fileWriter.write(input.getStringByField("value")+"\n");
            fileWriter.flush();
            System.out.println("输出成功");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void cleanup() {
        // TODO Auto-generated method stub

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        // TODO Auto-generated method stub

    }

    @Override
    public Map<String, Object> getComponentConfiguration() {
        // TODO Auto-generated method stub
        return null;
    }

}
